package com.inspur.ourhis_afterend.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inspur.ourhis_afterend.Dao.MemberDao;
import com.inspur.ourhis_afterend.Entity.Member;
import com.inspur.ourhis_afterend.Service.MemberService;
import com.inspur.ourhis_afterend.Tools.PageAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.Like;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    @Override
    public int add(Member member) throws Exception {
        if (member.getMemberid() == null) {
            return memberDao.insert(member);
        } else {
            return memberDao.updateById(member);
        }
    }

    @Override
    public int del(Integer memberid) throws Exception {
        return memberDao.deleteById(memberid);
    }

    @Override
    public IPage<Member> findAll(PageAction pageAction) {
        IPage<Member> iPage = new Page<>(pageAction.getPageNo(), pageAction.getPageSize());
        return memberDao.selectPage(iPage, null);
    }

    @Override
    public Member findOne(Integer memberid) throws Exception {
        return memberDao.selectById(memberid);
    }

    @Override
    public List<Member> search(String memberid, String realname, String credit, String tel) throws Exception {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("memberid", memberid).or().like("realname", realname).or().like("credit", credit).or().like("tel", tel);
        return memberDao.selectList(queryWrapper);
    }

    @Override
    public int increase(String memberid, double money) throws Exception {
        return memberDao.increase(memberid, money);
    }
}
